<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/7/27
 * Time: 上午6:38
 */

namespace App\Http\Controllers\V1;


use App\Http\Controllers\Controller;
use App\Providers\MyJwt;
use Illuminate\Http\Request;
use App\Services\RepairService;
use Illuminate\Support\Facades\Redis;

class AssetController extends Controller
{

    private $uid;

    public function __construct(Request $request)
    {
        parent::__construct();
        $authorization = $request->header('Authorization');
        $jwt = new MyJwt();
        $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);
        if (!$jwtCode || !array_key_exists('uid', $jwtCode)) {
            $this->sendJson([], 4031, '请登录');
        }
        if (!array_key_exists('role', $jwtCode) || $jwtCode['role'] != 4) {
            $this->sendJson([], 4032, '非法请求');
        }
        if (!array_key_exists('timeout', $jwtCode) || $jwtCode['timeout'] < time()) {
            $this->sendJson([], 4031, '请登录');
        }

        $cacheKey = sprintf('ht:login:uid:%d', $jwtCode['uid']);
        $cacheData = Redis::get($cacheKey);
        if (!$cacheData || $cacheData < time()) {
            Redis::setex($cacheKey, 0, null);
            $this->sendJson([], 4031, '请登录');
        }
        $this->jwtCode = $jwtCode;
        $this->uid = $jwtCode['uid'];
    }

    public function repairList(Request $request)
    {
        $this->sendGet(function() use($request) {
            $limit = $request->get('limit');
            $offset = $request->get('offset');
            $order = $request->get('order');
            $status = $request->get('status');
            $search = $request->get('search');
            $option = [];
            if ($status) {
                $option['query'][] = [
                    'key' => 'status',
                    'operator' => '=',
                    'value' => $status
                ];
            }
            if ($search) {
                $apartIds = $this->searchApartIdsByName($search);
                if (count($apartIds)) {
                    $option['query'][] = [
                        'key' => 'apart_id',
                        'operator' => 'in',
                        'value' => $apartIds
                    ];
                }
            }
            $data = RepairService::getRepairList($limit, $offset, $order, $option);
            $this->sendDataTable($data);
        });
    }

    public function repairDetail(Request $request)
    {
        $this->sendGet(function() use($request) {
            $id = $request->get('id');
            $data = RepairService::getRepairDetail($id);
            $this->sendJson($data);
        });
    }

    public function repairDelete(Request $request)
    {
        $this->sendPost(function() use($request) {
            $id = $request->get('id');
            RepairService::deleteRepair($id);
            $this->sendJson([], 200, '删除成功');
        });
    }

    public function changeRepairStatus(Request $request)
    {
        $this->sendPost(function() use($request) {
            $data = $request->get('data');
            RepairService::changeRepairStatus($data);
            $this->sendJson([], 200, '更新成功');
        });
    }
}